package com.jie.com.jie.controller;

import com.alibaba.fastjson.JSONObject;
import com.jie.com.feignclient.ProductClient;
import com.jie.com.jie.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: 张玉捷
 * @Date: 2021/5/8 20:43
 */
@RestController
public class CategoryController {

    @Autowired
    private ProductClient productClient;

    @GetMapping("/category")
    public String category(){
        System.out.println("category service....");

        String product = productClient.product();
        String list = productClient.list();
        String test = productClient.test("张三", 12);
        String test1 = productClient.test1("id111","李四");
        String test2 = productClient.test2(new Product(1,"衬衣",23.23,new Date()));
        String test3 = productClient.test3(new String[]{"21","22","23"});
        String test4 = productClient.test4(new String[]{"21","22","23"});


        System.out.println("结果一："+product);
        System.out.println("结果二："+list);
        System.out.println("结果三："+test);
        System.out.println("结果四："+test1);
        System.out.println("结果五："+test2);
        System.out.println("结果六："+test3);
        System.out.println("结果七："+test4);
        return "category ok"+test4;
    }

    @GetMapping("/category2")
    public Product product(){
        Product product1 = productClient.product(21);
        System.out.println("商品对象："+product1.toString());
        return product1;
    }

    @GetMapping("/category3")
    public List<Product> categoryList(){
        List<Product> products = productClient.findByCategoryId(1);
        products.forEach(product -> System.out.println("商品："+product));
        return products;
    }

    @GetMapping("/category4")
    public String productList(){
        String result = productClient.findbyCategoryIdAndPage(1,5,1);

        //自定义json反序列化，对象转为json 序列化，json转为对象 反序列化
        JSONObject jsonObject = JSONObject.parseObject(result);
        System.out.println(jsonObject.get("total"));
        System.out.println(jsonObject.get("rows"));

        //二次反序列化
        Object rows = jsonObject.get("rows");
        List<Product> products = jsonObject.parseArray(rows.toString(), Product.class);
        products.forEach(product -> System.out.println("商品："+product));

        return result;
    }
}
